package cn.shyouchi.wuguanjia.android.ui.workshow;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.show.ShowApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import mabeijianxi.camera.MediaRecorderActivity;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SendVideoWorkShowActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_show_name)
    EditText mEditShowName;
    @BindView(R.id.img_video)
    ImageView mImgVideo;


    private String videoUri;
    private String videoScreenshot;

    MenuItem mSendMenuItem;
    ShowApi mShowApi;


    @Override
    public int initContentView() {
        return R.layout.activity_send_video_work_show;
    }

    @Override
    public void initInjector() {
        mShowApi = new ShowApi(mContext);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());


        initData();

        mImgVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, VideoPlayerActivity.class).putExtra("path", videoUri));
            }
        });


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mSendMenuItem = menu.add("发送");
        mSendMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mSendMenuItem) {
            sendShow();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void sendShow() {

        if (TextUtils.isEmpty(mEditShowName.getText())) {
            ToastUtil.showToast("主题不能为空");
            return;
        }

        mShowApi.sendVideoShow(mEditShowName.getText().toString(), videoUri)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult baseRequestResult) {
                        finish();
                    }
                });
    }


    private void initData() {
        Intent intent = getIntent();
        videoUri = intent.getStringExtra(MediaRecorderActivity.VIDEO_URI);
        videoScreenshot = intent.getStringExtra(MediaRecorderActivity.VIDEO_SCREENSHOT);
        Bitmap bitmap = BitmapFactory.decodeFile(videoScreenshot);
        mImgVideo.setImageBitmap(bitmap);
    }
}
